﻿/* --------------------------------------------
 * 通用的上传
 ----------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FL.Common;
using System.IO;
using FL.DataImport;

namespace FL.Web.BackEnd
{
    public partial class UploadFile : FL.Web.BackEnd.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = GetStringFromQueryString("action", "");       //action
            Guid module = GetGuidFromQueryString("function_id");        //功能
            string limitExt = GetStringFromQueryString("limit", "");    //限制上传的文件类型
            string entity = GetStringFromQueryString("entity", "");     //要保存的表
            string field = GetStringFromQueryString("field", "");       //保存的对应的字段
            Guid parent_id = GetGuidFromQueryString("parent_id");       //指定的主表ID
            Upload(type, module, limitExt, entity, field);
        }

        public void Upload(string type, Guid moudle, string limit, string entity, string field)
        {
            if (type != "")
            {
                string[] filext = { "exe", "dll", "aspx", "asp", "jar", "com", "bat", "php", "jsp", "html", "htm" };
                HttpPostedFile postfile = FLContext.Request.Files[0];
                Response.Clear();
                if (Request.Files.Count > 0 && !FL.Common.Globals.IIFExistExtension(postfile.FileName, filext))
                {
                    //string output = @"<script type=""text/javascript"">window.parent.CKEDITOR.tools.callFunction({0} ,'{1}');</script>";
                    System.Web.HttpPostedFile f = Request.Files[0];
                    string filename = "/upload/" + entity + "/" + System.DateTime.Now.ToString("yyyyMMdd") + "/" + System.DateTime.Now.ToString("HHmmssfff") + f.FileName.Substring(f.FileName.LastIndexOf("."));
                    string filepath = FL.Common.FileHelper.GetFileDir(filename);
                    string fileExt = FL.Common.Globals.GetExtensions(filename);
                    string sourceFileName = f.FileName;
                    string dir = FL.Common.FileHelper.GetFileDir(Server.MapPath("~" + filename));
                    string root = Server.MapPath("~");
                    if (!System.IO.Directory.Exists(dir))
                    {
                        System.IO.Directory.CreateDirectory(dir);
                    }

                    string filePath = Server.MapPath("~" + filename);
                    f.SaveAs(Server.MapPath("~" + filename));
                    filename = FL.Common.FileHelper.GetFileNameNoPath(filename);

                    //output = string.Format(output, Request["CKEditorFuncNum"], filename);
                    FL.DBModule.DataTableBaseEntity dtentity = SaveFileData(type, moudle, entity, field, filename, filepath, fileExt, sourceFileName, postfile.ContentLength);

                    dtentity.EntityJson = FL.Common.Globals.DataRowToJSON(dtentity.DRow);
                    //数据导入
                    if (type == "import")
                    {
                        DataImport.DataImportBase import = new DataImportBase();
                        System.Data.DataTable dt = import.GetExeclTable(filePath);
                        import.Import(dt, entity);
                    }

                    Response.Write(dtentity.EntityJson);
                    Response.End();
                }
                else
                {
                    Response.Write("no file");
                    Response.End();
                }
            }
        }

        public FL.DBModule.DataTableBaseEntity SaveFileData(string type, Guid moudle, string entity, string field, string fileName, string filePath, string fileExt, string sourceFileName, int size)
        {
            FL.DBModule.DataTableBaseEntity dtentity = new FL.DBModule.DataTableBaseEntity();
            dtentity.TableName = "t_sys_files";
            dtentity.CreateDT();
            dtentity.SetValue("id", Guid.NewGuid());
            dtentity.SetValue("action_code", type);
            dtentity.SetValue("function_id", moudle);
            dtentity.SetValue("entity", entity);
            dtentity.SetValue("field", field);
            dtentity.SetValue("name", fileName);
            dtentity.SetValue("path", filePath);
            dtentity.SetValue("ext", fileExt);
            dtentity.SetValue("size", size);
            dtentity.SetValue("add_user", this.UserId);
            dtentity.SetValue("add_time", System.DateTime.Now);
            dtentity.SetValue("source_file_name", sourceFileName);
            _bll.entity = dtentity;
            _bll.Insert();
            return dtentity;
        }

        public FL.DBModule.DataTableBaseEntity SaveFileData(string type, Guid moudle, string entity, string field, string fileName, string filePath, string fileExt, string sourceFileName, int size,Guid parent_id)
        {
            FL.DBModule.DataTableBaseEntity dtentity = new FL.DBModule.DataTableBaseEntity();
            dtentity.TableName = "t_sys_files";
            dtentity.CreateDT();
            dtentity.SetValue("id", Guid.NewGuid());
            dtentity.SetValue("action_code", type);
            dtentity.SetValue("function_id", moudle);
            dtentity.SetValue("table_key_value", parent_id);
            dtentity.SetValue("entity", entity);
            dtentity.SetValue("field", field);
            dtentity.SetValue("name", fileName);
            dtentity.SetValue("path", filePath);
            dtentity.SetValue("ext", fileExt);
            dtentity.SetValue("size", size);
            dtentity.SetValue("add_user", this.UserId);
            dtentity.SetValue("add_time", System.DateTime.Now);
            dtentity.SetValue("source_file_name", sourceFileName);
            _bll.entity = dtentity;
            _bll.Insert();
            return dtentity;
        }
    }
}